<?php
include_once "header.php";

if(isset($_POST['kewenangan']) || isset($_POST['unit'])){

	header("Location:beranda.php");
}

if(isset($_POST["id"])){
		
	$daftar_unit = new Daftar_unit();
	$unit2 = $daftar_unit->getByEselon("II");
	$unit3 = $daftar_unit->getByEselon("III");
	$unit4 = $daftar_unit->getByEselon("IV");
	$pendisposisi = array_merge($unit2,$unit3);
	//var_dump($pendisposisi);
	$tujuan_disposisi = array_merge($unit3,$unit4);
	$kd_sm = $_POST["id"];
}

$cek_field="";

if(isset($_POST['pendisposisi'])){
	$daftar_unit = new Daftar_unit();
	$tujuan_disposisi = $daftar_unit->getByParent($_POST['pendisposisi']);
	if(empty($tujuan_disposisi)){$tujuan_disposisi = array_merge($unit3,$unit4);}
	
}

if (ISSET($_POST["simpan_disposisi"])){

	if($_POST['pendisposisi']=="" || empty($_POST['unit_tujuan'])){
	$cek_field="Peringatan: Silahkan isikan seluruh data yang tersedia.";
	
	} else {
		
		$tujuan = $_POST['unit_tujuan'];
		foreach($tujuan as $Tujuan){
			
				$daftar_disposisi = new Daftar_disposisi();
				$disposisi = new Disposisi();
				$disposisi->setKd_sm($_POST['id']);
				$disposisi->setPendisposisi($_POST['pendisposisi']);
				$disposisi->setTujuan_disposisi($Tujuan);
				$daftar_disposisi->add($disposisi);
				header("Location:detil_sm.php?id=".$_POST["id"]."");
			
		}
		
	}
}
?>

<div id="container">
	<div id="errorcont">
		<?php echo $cek_field;
		?>
	</div>
	<div id="maincont">
		<?php include_once "menu_samping.php"; ?>
		<div id="mainright">
			<div id="title">
			Surat Masuk > Detil > Rekam Disposisi
			</div>
			<div id="body">
			</br>
			<form name="form1" method="POST" action="rekam_disposisi.php">
			<input type="hidden" name="tambah_disposisi">
			<input type="hidden" name="id" value="<?php echo $kd_sm; ?>">
			<table border=0 cellspacing=0 cellpadding=4 width=100%>		
				<tr>
					<td>Pendisposisi</td>
					<td>:</td>
					<td>
						<select name="pendisposisi" onChange='this.form.submit()'>
							<option value="" >Pilih Pendisposisi</option>
							<?php
                                    foreach ($pendisposisi as $Pendisposisi)
                                    {
                                            if ($_POST['pendisposisi'] == $Pendisposisi->getKd_unit())
                                            echo "<option value=".$Pendisposisi->getKd_unit()." selected>Kepala ".ucwords($Pendisposisi->getUnit())."</option>";
                                           
                                            else
                                            echo "<option value=".$Pendisposisi->getKd_unit().">Kepala ".ucwords($Pendisposisi->getUnit())."</option>";
                                    }
                            ?>
						</select>
					</td>
				</tr>
				
				
				<tr>				
					<td>Tujuan Disposisi</td>
					<td>:</td>
					<td>
					<?php
					$i=1;
					foreach($tujuan_disposisi as $tujuan){ ?>
					<input type="checkbox" value="<?php echo $tujuan->getKd_unit(); ?>" name="<?php echo "unit_tujuan[]"; ?>" /><?php echo "Kepala ".ucwords($tujuan->getUnit()); ?>
					
					<?php echo "<br>"; $i++;} 
					?>
					<input type="hidden" name="jml_cek" value="<?php echo $i-1; ?>">
					
					</td>
				</tr>			
			</table>
			<br>
			<input type="submit" name="simpan_disposisi" value="Simpan"> 
			<input type="button" value="Batal" onClick="window.location.href='detil_sm.php?id=<?php echo $kd_sm; ?>'">
			</form>
						
			</div>
        </div>
	</div>
</div>
<?php
	include_once "footer.php";
?>

<script type="text/javascript">
	$(function() { 
	$("#tgl").datepicker({dateFormat: "yy-mm-dd",
											buttonImage:'../jquery/css/images/calendar.gif',
											buttonImageOnly: true,
											showOn: 'button'
	}); 
	});
</script>

